/*******************************************************************************
 * Copyright 2013 Mindaugas Idzelis
 * 
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 * 
 *   http://www.apache.org/licenses/LICENSE-2.0
 * 
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 ******************************************************************************/
package incontrol.bf3.packets;

import incontrol.bf3.protocol.events.EventType;
import incontrol.bf3.protocol.events.LevelLoadedEvent;
import incontrol.bf3.protocol.events.PlayerAuthEvent;
import incontrol.bf3.protocol.events.PlayerChatEvent;
import incontrol.bf3.protocol.events.PlayerJoinEvent;
import incontrol.bf3.protocol.events.PlayerKillEvent;
import incontrol.bf3.protocol.events.PlayerLeaveEvent;
import incontrol.bf3.protocol.events.PlayerSpawnEvent;
import incontrol.bf3.protocol.events.PlayerSquadChangeEvent;
import incontrol.bf3.protocol.events.PlayerStatsEvent;
import incontrol.bf3.protocol.events.PlayerTeamChangeEvent;
import incontrol.bf3.protocol.events.PunkbusterEvent;
import incontrol.bf3.protocol.events.RoundOverEvent;
import incontrol.bf3.protocol.events.ServerEvent;
import incontrol.bf3.protocol.events.TeamScoresEvent;
import incontrol.bf3.protocol.events.UnknownEvent;
import incontrol.bf3.protocol.events.datatype.PlayerInfo;
import incontrol.bf3.protocol.events.datatype.PlayerSet;
import incontrol.bf3.protocol.events.datatype.TeamScores;
import incontrol.rcon.protocol.Packet;

import java.util.Arrays;
import java.util.Collection;

public class EventFactory {

	public ServerEvent create(Packet packet) {
		String[] words = packet.getBodyAsString();
		EventType type = EventType.lookup(words[0]);
		switch (type) {
		case PLAYER_AUTH: {
			return new PlayerAuthEvent(words[1]);
		}
		case PLAYER_CHAT: {
			String source = words[1];
			String msg = words[2];
			PlayerSet target = PlayerSet.parse(Arrays.copyOfRange(words, 3,
					words.length));
			return new PlayerChatEvent(source, msg, target);
		}
		case PLAYER_JOIN: {
			String playerName = words[1];
			String guid = words[2];
			return new PlayerJoinEvent(playerName, guid);
		}
		case PLAYER_KILL: {
			String source = words[1];
			String target = words[2];
			String weapon = words[3];
			boolean headshot = Boolean.parseBoolean(words[4]);
			return new PlayerKillEvent(source, target, weapon, headshot);
		}
		case PLAYER_LEAVE: {
			String playerName = words[1];
			Collection<PlayerInfo> info = PlayerInfo.parse(Arrays.copyOfRange(
					words, 2, words.length));
			return new PlayerLeaveEvent(playerName, info.iterator().next());
		}
		case PLAYER_SPAWN: {
			String playerName = words[1];
			int team = Integer.parseInt(words[2]);
			return new PlayerSpawnEvent(playerName, team);
		}
		case PLAYER_SQUADCHANGE: {

			String playerName = words[1];
			int team = Integer.parseInt(words[2]);
			int squad = Integer.parseInt(words[3]);
			return new PlayerSquadChangeEvent(playerName, team, squad);
		}
		case PLAYER_TEAMCHANGE: {
			String playerName = words[1];
			int team = Integer.parseInt(words[2]);
			int squad = Integer.parseInt(words[3]);
			return new PlayerTeamChangeEvent(playerName, team, squad);
		}
		case PUNKBUSTER: {
			return new PunkbusterEvent(words[1]);
		}
		case SERVER_LEVELLOADED: {
			String levelName = words[1];
			String gameMode = words[2];
			int roundsPlayed = Integer.parseInt(words[3]);
			int roundsTotal = Integer.parseInt(words[4]);
			return new LevelLoadedEvent(levelName, gameMode, roundsPlayed,
					roundsTotal);
		}
		case SERVER_ROUNDOVER: {
			int team = Integer.parseInt(words[1]);
			return new RoundOverEvent(team);
		}
		case SERVER_ROUNDOVERPLAYERSTATS: {
			Collection<PlayerInfo> info = PlayerInfo.parse(Arrays.copyOfRange(
					words, 1, words.length));
			return new PlayerStatsEvent(info);
		}
		case SERVER_ROUNDOVERTEAMSTATS: {
			TeamScores scores = TeamScores.parse(Arrays.copyOfRange(words, 1,
					words.length));
			return new TeamScoresEvent(scores);
		}
		default: {
			return new UnknownEvent();
		}
		}
	}

}
